package com.calefk.core {
    import flash.display.MovieClip;
    import flash.events.Event;
	/**
     * Esta clase solo lo utiliza la clase Project, decide que cargar primero
     */
    public class LoadFiles {
        
        static public const ST_LOAD_SWF:int = 0;
        static public const ST_LOAD_XML:int = 1;
        private var mStateLoad:int = -1;
        
        private var mSWFManager:FileManager;
        private var mXMLManager:FileManager;
        private var mCurrentPercent:Number = 0;
        
        public var onEndLoad:Function;
        public var onProgressPercent:Function;
        
        public function get currentPercent():Number { return mCurrentPercent; }
         
        public function numFiles():int {
            return mSWFManager.numFiles + mXMLManager.numFiles;
        }
        
        public function LoadFiles(total_percent_swf:Number , total_percent_xml:Number ) {
            mSWFManager = new FileManager(FileManager.FILE_SWF , total_percent_swf);
            mXMLManager = new FileManager(FileManager.FILE_XML , total_percent_xml);
        }
        
        public function addFileXML(_filePath:String , _type:int,_objReference:*):void {
            mXMLManager.addFile(_filePath , _type ,_objReference);
        }
        
        public function addFileSWF(_filePath:String , _type:int,_objReference:*):void {
            mSWFManager.addFile(_filePath , _type ,_objReference);
        }       
		
        public function init(currentPercent:Number = 0):void {
            mCurrentPercent = currentPercent;
            loadNext();
        }
        
        private function loadNext():void {
            if (mSWFManager.numFiles > 0) {
                mSWFManager.loadFiles();
                mStateLoad = ST_LOAD_SWF;
                
            } else if (mXMLManager.numFiles > 0) {
                mXMLManager.loadFiles();
                mStateLoad = ST_LOAD_XML;
                
            } else {
                //mCanvas.removeEventListener(Event.ENTER_FRAME , onEnterFrame);
                //mCanvas.removeEventListener(IOErrorEvent.IO_ERROR , onIOErrorProject);                            
                onEndLoad();
            }            
        }
        
        public function update():void {
            switch (mStateLoad) {
                case ST_LOAD_SWF:
                    if (mSWFManager.isLoadAllFiles) {
                        mCurrentPercent += mSWFManager.totalPercent;

                        if (mXMLManager.numFiles > 0) {
                            
                            mXMLManager.loadFiles();
                            mStateLoad = ST_LOAD_XML;
                            
                        } else {
                            //mCanvas.removeEventListener(Event.ENTER_FRAME , onEnterFrame);
                            //mCanvas.removeEventListener(IOErrorEvent.IO_ERROR , onIOErrorProject);                            
                            onEndLoad();
                        }
                    } else {
                        // ------- loading percent ----------
                        onProgressPercent(mCurrentPercent + mSWFManager.currentPercentLoaded , 100);                        
                        
                    }
                    break;
                case ST_LOAD_XML:
                    if (mXMLManager.isLoadAllFiles) {
                        mCurrentPercent += mXMLManager.totalPercent;
                        
                        // ultima llamada del progress
                        onProgressPercent(mCurrentPercent , 100);
                        
                        // mCanvas.removeEventListener(Event.ENTER_FRAME , onEnterFrame);
                        // mCanvas.removeEventListener(IOErrorEvent.IO_ERROR , onIOErrorProject);                            
                        onEndLoad();
                    } else {
                        // ------- loading percent ----------
                        onProgressPercent(mCurrentPercent + mXMLManager.currentPercentLoaded , 100);                        
                                           
                    }
                    break;
            }
        }
		
		public function clean():void {
			mSWFManager.clean();
			mXMLManager.clean();
			onEndLoad = null;
			onProgressPercent = null;
		}
        
    }

}